<?php

namespace app\api\controller;

use app\admin\model\District;
use app\common\controller\Api;
use app\common\model\Area;
use think\Request;

/**
 * 地址接口
 */
class Transport extends Api
{
    protected $noNeedLogin = '';
    protected $noNeedRight = ['*'];

    protected $model = null;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->model = new \app\admin\model\Transport();
    }

    /**
     * 获取地址列表
     * @ApiSummary  (WanlShop 地址接口获取地址列表)
     * @ApiMethod   (GET)
     *
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $list = $this->model
            ->where('user_id', $this->auth->id)
            ->field('id,realname,mobile,area,address,status')
            ->order('id desc')
            ->select();

        if ($list) {
            foreach ($list as $val) {
                $district = District::field('id,cid,pid,name')->where('cid', $val->area)->find();
                $city = District::field('id,cid,pid,name')->where('id', $district->pid)->find();
                $province = District::field('id,cid,name')->where('id', $city->pid)->find();
                if (!$province) {
                    $province = $city;
                }
                $val['province'] = $province->name;
                $val['city'] = $city->name;
                $val['district'] = $district->name;
            }
        }

        $this->success('地址列表为', $list);
    }

    /**
     * 修改/新增地址
     * @throws \think\Exception
     */
    public function address()
    {
        if (!$this->request->isPost()) {
            $this->error('非法操作');
        }
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $data = $this->request->post();
        if (!$data['mobile'] || !$data['area'] || !$data['address'] || !$data['realname']) {
            $this->error('参数缺失！');
        }

        $id = $data['id'];
        unset($data['token'], $data['type']);
        $data['user_id'] = $this->auth->id;

        $count = $this->model->where(['user_id' => $data['user_id']])->count();
        if ($id) {
            $row = $this->model->find($id);
            if (!$row) {
                $this->error('该地址不存在！');
            }
            if ($count == 0) {
                $data['status'] = 1;
                $this->model->allowField(true)->save($data, ['id' => $data['id']]);
            } else {
                if ($data['status'] == 1) {
                    $this->model->where('user_id', $data['user_id'])->setField('status', 0);
                }
                $this->model->allowField(true)->save($data, ['id' => $data['id']]);
            }

            $this->success('编辑成功');
        } else {
            if ($count == 0) {
                // 新增
                $data['status'] = 1;
                $this->model->data($data);
                $this->model->save();
            } else {
                if ($data['status'] == 1) {
                    $this->model->where('user_id', $data['user_id'])->setField('status', 0);
                }
                $this->model->data($data);
                $this->model->save();
            }
            $this->success('添加成功');
        }
    }

    /**
     * 地址详情
     */
    public function read()
    {
        if (!$this->request->isPost()) {
            $this->error('非法操作');
        }
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $id = $this->request->post('id');
        if (!$id) {
            $this->error('参数错误');
        }
        $userId = $this->auth->id;
        $row = $this->model->field('id,realname,mobile,area,address,status')->where('id', $id)->where('user_id', $userId)->find();
        if (!$row) {
            $this->error('该地址不存在!');
        }

        $district = District::field('id,cid,pid,name')->where('cid', $row->area)->find();
        $city = District::field('id,cid,pid,name')->where('id', $district->pid)->find();
        $province = District::field('id,cid,name')->where('id', $city->pid)->find();
        if (!$province) {
            $province = $city;
        }
        $row['province'] = $province->name;
        $row['city'] = $city->name;
        $row['district'] = $district->name;

        $this->success('地址详情为', $row);
    }

    /**
     * 删除地址
     */
    public function delete()
    {
        if (!$this->request->isPost()) {
            $this->error('非法操作');
        }

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $id = $this->request->post('id');
        if (!$id) {
            $this->error('参数错误');
        }
        $userId = $this->auth->id;
        $row = $this->model->where('id', $id)->where('user_id', $userId)->find();
        if (!$row) {
            $this->error('该地址不存在!');
        }

        $res = $this->model->where('id', $id)->delete();
        if (!$res) {
            $this->error('删除失败！');
        }

        $this->success('删除成功！');
    }

    /**
     * 获取默认地址
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function readd()
    {
        $userId = $this->auth->id;
        $row = $this->model->field('id,realname,mobile,area,address,status')->where('status', 1)->where('user_id', $userId)->find();
        if (!$row) {
            $this->error('暂无默认地址!');
        }

        $district = District::field('id,cid,pid,name')->where('cid', $row->area)->find();
        $city = District::field('id,cid,pid,name')->where('id', $district->pid)->find();
        $province = District::field('id,cid,name')->where('id', $city->pid)->find();
        if (!$province) {
            $province = $city;
        }
        $row['province'] = $province->name;
        $row['city'] = $city->name;
        $row['district'] = $district->name;

        $this->success('地址详情为', $row);
    }
}
